FROM centos:centos7.5.1804


# ensure local python is preferred over system one
ENV PATH="/usr/local/bin:$PATH"

# set locale
ENV LANG="C.UTF-8"


# switch repo mirrors; default mirror is no longer maintained
COPY Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo


# set timezone
RUN set -eux; \
    yum makecache; \
    yum -y update; \
    yum install -y tzdata

ENV TZ="Asia/Shanghai"


# build dependencies
RUN set -eux; \
    yum -y groupinstall "Development Tools"; \
    yum -y install \
        gcc \
        gcc-c++ \
        make \
        zlib-devel \
        bzip2 \
        bzip2-devel \ 
        readline-devel \
        openssl-devel \
        xz \
        xz-devel \
        libffi-devel \
        tcl \
        which 

WORKDIR /


# build sqlite
ENV SQLITE_VERSION="3.37.0"

# remove system sqlite3 temporarily
RUN set -eux; \
    rm /usr/bin/sqlite3; \
    rm /usr/lib64/libsqlite3.so.0; \
    rm /usr/lib64/libsqlite3.so.0.8.6

RUN mkdir -p /opt/sqlite3
COPY sqlite-version-${SQLITE_VERSION}.tar.gz /

RUN set -eux; \
    tar xvf sqlite-version-${SQLITE_VERSION}.tar.gz; \
    cd sqlite-version-${SQLITE_VERSION}; \
    ./configure --prefix=/opt/sqlite3; \
    make -j4; \
    make install


# build python
ENV PYTHON_VERSION="3.7.17"

RUN mkdir -p /opt/python3
COPY Python-${PYTHON_VERSION}.tgz /

RUN set -eux; \
    tar xvf Python-${PYTHON_VERSION}.tgz; \
    cd Python-${PYTHON_VERSION}; \
    LD_RUN_PATH="/opt/sqlite3/lib" \
    LDFLAGS="-L/opt/sqlite3/lib" \ 
    CPPFLAGS="-I/opt/sqlite3/include" \
    ./configure \ 
        --prefix=/opt/python3/ \
        --enable-optimizations; \
    make -j4


WORKDIR /Python-${PYTHON_VERSION}/
CMD LD_RUN_PATH="/opt/sqlite3/lib" make install && cp -r /opt/sqlite3 /opt/_sqlite3

